const config = {
  api: {
    endpoints: { prod: 'https://api.prod.com' },
    timeout: 5000
  },
  features: {
    flags: ['feature_a', 'feature_b', 'feature_c'],
    settings: { 'enable-cache': true }
  },
  users: [] // 空数组，用于测试边界
};

function getValue(data, path, defaultValue = undefined) {
  if (!path) {
    return defaultValue
  }

  path = path.replace(/\.\.+/g, '.').replace(/\[(\d+)\]/g, '.$1')

  const pathArr = path.split('.')
  
  let current = data
  for (let key of pathArr) {
    if (Array.isArray(current) && key < current.length) {
      if (key>= 0) {
        current = current[key]
      } else if (current.length + key >= 0) {
        current = current[current.length + key]
      } else {
        return defaultValue
      }
    } else if (key in current) {
      current = current[key]
    } else {
      return defaultValue
    }
  }
  return current
}

console.log(getValue(config, 'api.endpoints.prod'));  // features.flags.2